<template>
  <basic-container>
    <div class="card-border">
      <card-header :header-data="headerData"></card-header>
      <avue-crud :data="levyResultData"
                 :option="tableoption"
                 :page="page"
                 :table-loading="tableLoading"
                 @size-change="sizeChange"
                 @current-change="currentChange"
                 @search-change="searchChange"
                 @search-reset="searchReset"
                 class="avuecrud">
        <template slot="menuRight">
          <el-button type="warning" size="small" @click="exportToDoCollectionData()">导出</el-button>
        </template>
        <template slot-scope="scope" slot="menu">
          <div class="menu-title" style="padding: 9.5px 0px">
            <i class="iconfont  icon-primary"></i>
            <p class="menu-title-text"></p>
          </div>
          <div class="menu-title" @click.stop="collectionFeedback(scope.row,scope.index)"
               v-if="scope.row.state!='3'&&!scope.row.isEnd">
            <i class="iconfont iconalter icon-warning"></i>
            <p class="menu-title-text">反馈</p>
          </div>
        </template>
      </avue-crud>
      <el-dialog title="材料收集详情" :visible.sync="dialogVisible" width="70%">
        <div style="margin-top: -48px">
          <bostech-detail :detail-data="detailData"></bostech-detail>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">关 闭</el-button>
          </span>
      </el-dialog>
    </div>
  </basic-container>
</template>

<script>
  import {levyDelete} from "@/api/subject";
  import {getMaterialDetail, getmaterialCollectionSingle, getTodoList,exportToDoCollectionData,exportToDoCollectionDataExcel} from "@/api/fileTask";
  import {getNotNull} from '@/views/util';
  import {getfileinfopage} from "@/api/system"
  import {FILETASK_DIC} from "@/views/fileTask/const";

  export default {
    name: "todoCollection",
    created() {
      var self = this;
      this.getResultList(this.page);
      window.getMaterialDetail = self.getMaterialDetail
    },
    data() {
      return {
        headerData: {
          title: '材料收集'
        },
        levyResultData: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        tableoption: {
          page: true,
          menuWidth: 100,
          addBtn: false,
          editBtn: false,
          addRowBtn: false,
          indexLabel: "序号",
          index: true,
          indexWidth:70,
          cellBtn: false,
          delBtn: false,
          columnBtn: false,
          refreshBtn: false,
          filterBtn: false,
          searchBtn: false,
          keyId: "id",
          column: [{
            label: "材料收集主题",
            prop: "title",
            cell: true,
            formatter: function (row, value, label, column) {
              return '<a style="cursor: pointer;" onclick="getMaterialDetail(\'' + row.fileTaskId + '\')">' + value + '</a>';
            },
          },{
            label: "主题名称",
            prop: "titleLike",
            search: true,
            hide: true,
            cell: true,
            formatter: function (row, value, label, column) {
              return '<a style="cursor: pointer;" onclick="getMaterialDetail(\'' + row.fileTaskId + '\')">' + value + '</a>';
            },
          },
            {
              label: "发起征集部门",
              prop: "deptName",
              width: 120,
              cell: true
            },
            {
              label: "截止时间",
              prop: "endDate",
              width: 120,
              cell: true,
            },
            {
              label: "类型",
              width: 110,
              prop: "type",
              cell: true,
              type: "select",
              dicData: FILETASK_DIC.TYPE,
            },
            {
              label: "状态",
              width: 80,
              prop: "state",
              cell: true,
              formatter: function (row, value, label, column) {
                if (row.isEnd) {
                  return "<font color='#7EBE50'>" + "已结束" + "</font>";
                } else {
                  if (row.state == '0') {
                    return "<font color='red'>" + "催办" + "</font>";
                  } else if (row.state == '1') {
                    return "<font color='red'>" + "待反馈" + "</font>";
                  } else if (row.state == '2') {
                    return "<font color='#DCA450'>" + "补充" + "</font>";
                  } else if (row.state == '3') {
                    return "<font color='#7EBE50'>" + "已反馈" + "</font>";
                  }
                }
              }
            },
          ]
        },
        tableLoading: false,
        dialogVisible: false,
        detailData: [],
        fileIdstemp: [],
        titleLikeSearch:''
      }
    },
    methods: {
      // 课题详情跳转
      collectionFeedback(row) {
        let src = `/collectionFeedback/detail/${row.fileTaskId}`;
        this.$router.push({
          path: src,
          query: {
            singleOrNot: '1',
            state: row.state,
            type: row.type,
            toDoOrnot: '0',
          }
        })
      },
      // 课题详情跳转
      viewLevyDeptDetail(row) {
        let src = `/levyFeedback/detail/${row.collectionId}`;
        this.$router.push({
          path: src,
          query: {
            targetId: row.targetId,
            toDoOrnot: '1'
          }
        })
      },
      levyDelete(row) {
        this.$confirm("确定删除此课题征集吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          levyDelete(row.collectionId).then(response => {
            if (response.data.code == 0) {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              });
              this.$router.go(0);
            }
          })
        })
      },
      currentChange(currentPage) {
        var page = this.page;
        page.currentPage = currentPage;
        let params = {
          titleLike:this.titleLikeSearch,
        };
        this.getResultList(page,params);
      },
      sizeChange(pageSize) {
        var page = this.page;
        page.pageSize = pageSize;
        let params = {
          titleLike:this.titleLikeSearch,
        };
        this.getResultList(page,params);
      },
      searchReset() {
        this.titleLikeSearch = '';
        let params = {
          titleLike:this.titleLikeSearch,
        };
        this.getResultList(this.page,params);
      },
      //搜索回调
      searchChange (params) {
        this.page.currentPage = 1
        this.getResultList(this.page, this.dealSearchParam(params));
      },
      // 处理搜索过滤
      dealSearchParam(params) {
        if (params.titleLike) {
          params.titleLike = params.titleLike;
        }
        this.titleLikeSearch = params.titleLike;
        return params;
      },
      getResultList(page, params) {
        getTodoList(Object.assign({
          current: page.currentPage,
          size: page.pageSize,
          isEnd:false,
        }, params)).then(response => {
          this.levyResultData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.$emit("getTotal", response.data.data.total)
        })
      },
      async getMaterialDetail(fileTaskId) {
        await getMaterialDetail(fileTaskId).then(response => {
          if (response.data.code == '0') {
            var records = response.data.data;
            this.detailData = [
              {label: '材料收集名称', value: getNotNull(records.title)},
              {label: '截止时间', value: getNotNull(records.endDate)},
              {label: '所需材料描述', value: getNotNull(records.description)},
            ];
            this.fileIdstemp = records.fileIds;
            this.dialogVisible = true;
          }
        });
        if (this.fileIdstemp !== null) {
          if (this.fileIdstemp.length != 0) {
            getfileinfopage(Object.assign({
              current: 1,
              size: 100,
              fileIds: this.fileIdstemp.join(",")
            })).then(response => {
              if (response.data.code == '0') {
                var multiplefiles = response.data.data.records;
                this.detailData.push({label: '附件', fileName: multiplefiles, type: 'related'});
              }
            })
          }
        }
      },
      exportToDoCollectionData(){
        this.$confirm("确定要导出材料收集列表吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          exportToDoCollectionDataExcel(Object.assign({
            isEnd:false,
          })).then(response => {
            let exportFileName = "材料收集列表.xlsx";
            this.download(response.data, exportFileName)
          });
        })
      },
      // 下载文件
      download(data, filename) {
        if (data == null) {

        } else {
          let url = window.URL.createObjectURL(new Blob([data]));
          let link = document.createElement('a');
          link.style.display = 'none';
          link.href = url;
          link.setAttribute('download', filename);
          document.body.appendChild(link);
          link.click()
        }
      },
    }
  }
</script>

<style lang="scss" scoped>
  .card-body {
    padding: 10px;
    min-height: 400px
  }
</style>
